function yhfindBall(rn = 6, bn = 1) {
  let rball = [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
    22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
  ];
  let bball = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
  let rq = [
    5, 6, 9, 5, 2, 0, 6, 5, 1, 3, 8, 9, 7, 2, 3, 5, 6, 0, 9, 7, 2, 3, 5, 8, 3,
    9, 1, 6, 0, 8, 3, 6, 6,
  ];
  let bq = [8, 8, 3, 4, 7, 4, 7, 1, 8, 6, 1, 5, 3, 5, 9, 6];

  let rqh = [];

  let bqh = [];

  let r = []; //选中红球
  let b = []; //选中蓝球

  //选红球
  // 选红球
  while (r.length < rn) {
    //重新计算权重和
    rqh = [0];
    for (let i = 0; i < rq.length; i++) {
      rqh[i + 1] = rqh[i] + rq[i];
    }
    //随机数 [0 ~ 权重和)
    let rnum = Math.floor(Math.random() * rqh[rqh.length - 1]);
    let index = efFind(rqh, rnum);
    if (!r.includes(rball[index])) {
      r.push(rball[index]);
    }
    rq.splice(index, 1);
    rball.splice(index, 1);
  }
  // 选篮球
  while (b.length < bn) {
    bqh = [0];
    for (let i = 0; i < bq.length; i++) {
      bqh[i + 1] = bqh[i] + bq[i];
    }
    //随机数 1 ~ 权重和
    let bnum = Math.floor(Math.random() * bqh[bqh.length - 1]);
    let index = efFind(bqh, bnum);
    if (!b.includes(bball[index])) {
      b.push(bball[index] + "");
    }
    bq.splice(index, 1);
    bball.splice(index, 1);
  }

  //二分边界查找
  function efFind(arr, target) {
    let left = 0;
    let right = arr.length;
    while (left < right) {
      let mid = left + Math.floor((right - left) / 2);
      let m = arr[mid];
      if (m == target) {
        return mid;
      } else if (m > target) {
        right = mid;
      } else if (m < target) {
        left = mid + 1;
      }
    }
    return left - 1;
  }
  r.sort((a, bb) => a - bb);
  b.sort((a, bb) => a - bb);
  return [...r, ...b];
}
for (let i = 0; i < 10; i++) {
  // console.log(yhfindBall());
}

let xlsx = require("node-xlsx");
let fs = require("fs");
let path = require("path");

function writeExcel(data, fileName) {
  let buffer = xlsx.build([
    {
      name: "文件yi",
      data: [
        [1, 2, 3, 4, 5],
        [6, 7, 8, 9, 10],
      ],
    },
    {
      name: "文件er",
      data: [
        [11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20],
      ],
    },
  ]);
  fs.writeFileSync(path.join(__dirname, `/aa.xlsx`), buffer, { flag: "w" });
}
writeExcel();
